package com.wing.warehouse.controller.admin.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.warehouse.dao.WarehousePalletBoxListDao;
import com.wing.warehouse.model.entity.PalletBoxListEntity;
import com.wing.warehouse.model.entity.PalletBoxListVO;
import com.wing.warehouse.model.request.PalletBoxListForm;
import com.wing.util.BarCodeGen;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;

/**
 * 托盘料箱清单-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class WarehousePalletBoxListServiceImpl extends ServiceImpl<WarehousePalletBoxListDao, PalletBoxListEntity> implements WarehousePalletBoxListService {

    @Override
    @Transactional(readOnly = false)
    public boolean add(PalletBoxListForm palletBoxListForm) {
        //校验

        PalletBoxListEntity palletBoxList =new PalletBoxListEntity();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(PalletBoxListEntity.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(palletBoxListForm, palletBoxList, copyOptions);

        String prefix = palletBoxListForm.getPrefix(); // 前缀
        List<PalletBoxListEntity> palletBoxLists = new ArrayList<>();
        for (int j = 0; j < palletBoxListForm.getQuantity(); j++) {
            PalletBoxListEntity boxList =new PalletBoxListEntity();
            boxList.setPrefix(prefix);
            boxList.setVehicleTypeId(palletBoxList.getVehicleTypeId());
            boxList.setVehicleStandardSizesId(palletBoxList.getVehicleStandardSizesId());
            boxList.setTrayPhoto(palletBoxList.getTrayPhoto());
            boxList.setRemark(palletBoxList.getRemark());
            boxList.setCode(prefix+"-"+(j+1));
            palletBoxLists.add(boxList);
        }
        return saveBatch(palletBoxLists);
    }

    @Override
    public List<PalletBoxListVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<PalletBoxListVO> pageList4app(Page page, Map<String, Object> params) {
        List<PalletBoxListVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public PalletBoxListVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public String getBarCode(String code) {
        try{
            byte[] barcodeBytes = BarCodeGen.generateBarCode128(code, 10.00, 0.3, true, true);
            String barcodeBase64Val= Base64.getEncoder().encodeToString(barcodeBytes);
            return "data:image/png;base64,"+barcodeBase64Val;
        }catch (Exception e){
            log.error("gen barcode fail",e);
        }
        return "";
    }

    @Override
    public PalletBoxListVO getPalletBox(String code,Long warehouseId) {
        return baseMapper.getPalletBox(code,warehouseId);
    }

    @Override
    public PalletBoxListVO getPalletBoxByLabelCode(String labelCode) {
        return baseMapper.getPalletBoxByLabelCode(labelCode);
    }

    @Override
    public List<PalletBoxListVO> getBoxByDeliveryDetailId(Long deliveryDetailId) {
        return baseMapper.getBoxByDeliveryDetailId(deliveryDetailId);
    }

    @Override
    public List<PalletBoxListVO> getPLanProductBoxByJobProductId(String skuCode, Long warehouseId) {
        return baseMapper.getOutWorkPalletBoxList(skuCode,warehouseId);
    }

    @Override
    public List<PalletBoxListVO> getOutWorkPalletBoxList(String skuCode, Long warehouseId) {
        return baseMapper.getOutWorkPalletBoxList(skuCode,warehouseId);
    }
}
